/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controle;

import dao.Dao;
import entidades.Campeonato;
import entidades.TimeFutebol;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import util.Upload;

/**
 *
 * @author Marco Antonio
 */
@WebServlet(name = "ServletTimes", urlPatterns = {"/ServletTimes"})
public class ServletTimes extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ServletTimes</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ServletTimes at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String operacao = request.getParameter("op");
        int id = Integer.parseInt(request.getParameter("id"));
        if (operacao.equals("remover")) {
            new Dao<TimeFutebol>(TimeFutebol.class).remove(id);
        } else if (operacao.equals("editar")) {
            HttpSession session = request.getSession(false);
            session.setAttribute("time", new Dao<TimeFutebol>(TimeFutebol.class).get(id));
        }
        response.sendRedirect("times.jsp");
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        
        //Cria um objeto upload já setando e criando o diretorio que os
        // arquivos serão armazenados
        Upload upload = new Upload(getServletContext().getRealPath(ServletGeral.DIR));

        // Retorna um list com todos os componentes de uma requisição
        List list = upload.processRequest(request);

        // Neste Map você obteve todos os campos do formulário.
        Map<String, String> map = upload.getFormValues(list);
        // Colocando cada campo do formulário em uma variável
        // Para pegar um campo dentro do map tem que passar o id do campo
        String escudo = map.get("escudo");
        String nome = map.get("nome");
        String operacao = map.get("op");


        // Tratando operação
        if (operacao.equals("Salvar")) {
            TimeFutebol t = new TimeFutebol();

            t.setNome(nome);

            t.setEscudo(escudo);

            new Dao<TimeFutebol>(TimeFutebol.class).insert(t);

        } else if (operacao.equals("Editar")) {
            HttpSession session = request.getSession(false);
            TimeFutebol t = (TimeFutebol) session.getAttribute("time");

            t.setNome(nome);
            if (escudo != null) {
                t.setEscudo(escudo);
            }

            new Dao<TimeFutebol>(TimeFutebol.class).update(t);
            session.removeAttribute("time");
        }
        response.sendRedirect("classificacao.jsp");
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
